<?php

namespace App\Admin\Forms;

use Dcat\Admin\Widgets\Form;
use App\Admin\Field\IntervalWeekNum;
use App\Models\HospitalAppointInterval;
use App\Models\HospitalAppointIntervalNum;
use Illuminate\Support\Facades\DB;

class HospitalAppointVaccineForm extends Form
{
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {

        return $this->response()->success('成功');
    }

    /**
     * Build a form here.
     */
    public function form()
    {

        $form = $this;
        $form->tab('疫苗设置', function (Form $form) {
            $form->html('设置后疫苗仅可在已选门诊日预约，如不需要用户预约时选择具体疫苗，那么此处选择“儿童疫苗”即可');
            $form->select('vaccine_id')->options(\App\Models\Vaccine::typeLabels('id', 'name'));
            $form->html('注：如不需要用户预约时选择具体疫苗，那么此处选择“儿童疫苗”即可');

            $form->multipleSelect('weeks', '门诊日')->options([
                '1' => '周一  ',
                '2' => '周二  ',
                '3' => '周三  ',
                '4' => '周四  ',
                '5' => '周五  ',
                '6' => '周六  ',
                '0' => '周日  ',
            ])->saving(function ($value) {
                return json_encode($value);
            });
            $form->hidden('hospital_appoint_id');
            $form->hidden('hospital_id');
            $form->text('intervals')->value(11);
        })->tab('疫苗门诊日号源', function (Form $form) {
            $form->html('设置后用户选择此疫苗时门诊日最多可约数量，且不会超过该项目门诊日的总数，未设置则按照门诊日总数判断');

            $form->embeds('week_num', '', function ($form) {
                $form->text('week1');
                $form->text('week2');
                $form->text('week3');
                $form->text('week4');
                $form->text('week5');
                $form->text('week6');
                $form->text('week0');
            })->saving(function ($value) {
                return json_encode($value);
            });
        })->tab('疫苗门诊日时段号源', function (Form $form) {

            $intervals = HospitalAppointInterval::query()->where('hospital_appoint_id', $form->model()->hospital_appoint_id)->get();
            if ($intervals) {
                $typeText = [];
                foreach ($intervals as $k => $v) {
                    $typeText[$v->id] = $v->start . "-" . $v->end;
                }
                $weeks = [1, 2, 3, 4, 5, 6, 0];

                $row = [];
                foreach ($typeText as $k => $v) {
                    $row[$k]['title'] = $v;
                    $row[$k]['key'] = $k;

                    foreach ($weeks as $wk => $wv) {
                        $wArr[$wv]['open'] = 0;
                        $wArr[$wv]['value'] = "";
                        $wArr[$wv]['key'] = $wv;
                    }
                    $row[$k]['weeks'] = $wArr;
                }
            }
            $form->intervalweeknum('interval_num')->value($row);
            $form->html('设置后用户选择此疫苗时门诊日时段最多可约数量，且不会超过该项目时段、门诊日的总数，未设置则按照门诊日总数判断');
            $form->html('勾选开启，表示选择该疫苗时只能预约已勾选开启的时段');
            $form->html('勾选开启，数量设置为空则不限制（以项目时间段号源为准），为0则不可约，为10则该疫苗只能在此时段预约10个');
            $form->html('不勾选开启，只设置数量当前疫苗将不会生成排班，用户不可预约');

        });
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        return [
            'name'  => 'John Doe',
            'email' => 'John.Doe@gmail.com',
        ];
    }
}
